<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <!-- c3p0 database connection pool management -->
    <bean id="dataSourceTest" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cadetlogintest?useSSL=false"/>
        <property name="user" value="root"/>
        <property name="password" value=""/>

        <!-- c3p0 database connection pool management properties -->
        <property name="minPoolSize" value="1"/>
        <property name="maxPoolSize" value="10"/>
        <property name="acquireIncrement" value="1"/>
        <property name="maxStatements" value="0"/>

        <!-- destroy idle pool after 300ms -->
        <property name="idleConnectionTestPeriod" value="300"/>

        <!-- destroy db connection if it fails to login after 7200ms -->
        <property name="loginTimeout" value="7200"/>
    </bean>

    <!-- spring jpa entity manager factory configuration -->
    <bean id="emFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSourceTest"/>
        <property name="packagesToScan" value="org.academiadecodigo.bootcamp.fxlogin.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
                <entry key="hibernate.show_sql" value="true"/>
                <!-- WARNING: Will re-create the database schema, causing data loss -->
                <!--<entry key="hibernate.hbm2ddl.auto" value="update"/>-->
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="sessionManager" class="org.academiadecodigo.bootcamp.fxlogin.persistence.jpa.JpaSessionManager"
          destroy-method="close">
        <constructor-arg name="emf" ref="emFactory"/>
    </bean>

    <bean id="jpaTransactionManager" class="org.academiadecodigo.bootcamp.fxlogin.persistence.jpa.JpaTransactionManager">
        <constructor-arg name="sm" ref="sessionManager"/>
    </bean>

</beans>